import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:smart_translator/app/utils/CommonUtil.dart';

import '../../../event/DataCallBack.dart';
import '../../../nets/NetWorkManager.dart';
import '../../../routes/app_pages.dart';
import '../../../utils/tips.dart';
import '../../login/controllers/login_controller.dart';

class ForgotValidateCodeController extends GetxController {

  var focusNode1 = FocusNode().obs;
  var focusNode2 = FocusNode().obs;
  var focusNode3 = FocusNode().obs;
  var focusNode4 = FocusNode().obs;
  var focusNode5 = FocusNode().obs;
  var focusNode6 = FocusNode().obs;

  var textEditor1 = TextEditingController().obs;
  var textEditor2 = TextEditingController().obs;
  var textEditor3 = TextEditingController().obs;
  var textEditor4 = TextEditingController().obs;
  var textEditor5 = TextEditingController().obs;
  var textEditor6 = TextEditingController().obs;

  var hiddenUnderLine1 = false.obs;
  var hiddenUnderLine2 = false.obs;
  var hiddenUnderLine3 = false.obs;
  var hiddenUnderLine4 = false.obs;
  var hiddenUnderLine5 = false.obs;
  var hiddenUnderLine6 = false.obs;

  var account = ''.obs;
  var password = ''.obs;
  var isEmailType =  false.obs;

  var isBtnEnable = false.obs;

  var isCheck = false.obs;
  var isCheckSuccess = false.obs;

  var timeCount = 60.obs;
  var showTimeText = false.obs;
  Timer? _timer;

  @override
  void onInit() {
    super.onInit();
    account.value = Get.arguments['account'] as String;
    password.value = Get.arguments['passwd'] as String;

    isEmailType.value = CommonUtil.isEmail(account.value);

    textEditor1.value.text ='';
    textEditor2.value.text ='';
    textEditor3.value.text ='';
    textEditor4.value.text ='';
    textEditor5.value.text ='';
    textEditor6.value.text ='';

    hiddenUnderLine1.value = false;
    hiddenUnderLine2.value = false;
    hiddenUnderLine3.value = false;
    hiddenUnderLine4.value = false;
    hiddenUnderLine5.value = false;
    hiddenUnderLine6.value = false;
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void changeFocus() {
    hiddenUnderLine1.value =  textEditor1.value.text.trim() == '' ? false: true;
    hiddenUnderLine2.value =  textEditor2.value.text.trim() == '' ? false : true;
    hiddenUnderLine3.value =  textEditor3.value.text.trim() == '' ? false : true;
    hiddenUnderLine4.value =  textEditor4.value.text.trim() == '' ? false : true;
    hiddenUnderLine5.value =  textEditor5.value.text.trim() == '' ? false : true;
    hiddenUnderLine6.value =  textEditor6.value.text.trim() == '' ? false : true;

    if (textEditor1.value.text == '') {
      focusNode1.value.requestFocus();
    }

    if (textEditor1.value.text != '' && textEditor2.value.text == '') {
      focusNode1.value.unfocus();
      focusNode2.value.requestFocus();
    }

    if (textEditor2.value.text != '' && textEditor3.value.text == '') {
      focusNode2.value.unfocus();
      focusNode3.value.requestFocus();
    }

    if (textEditor3.value.text != '' && textEditor4.value.text == '') {
      focusNode3.value.unfocus();
      focusNode4.value.requestFocus();
    }

    if (textEditor4.value.text != '' && textEditor5.value.text == '') {
      focusNode4.value.unfocus();
      focusNode5.value.requestFocus();
    }

    if (textEditor5.value.text != '' && textEditor6.value.text == '') {
      focusNode5.value.unfocus();
      focusNode6.value.requestFocus();
    }

    if (textEditor6.value.text != '') {

      String validateCode = textEditor1.value.text+textEditor2.value.text+textEditor3.value.text+textEditor4.value.text+
          textEditor5.value.text+textEditor6.value.text;
      if (isEmailType.value) {
        NetWorkManager.getInstance().requestBussiness(NetWorkManager.FORGET_EMAIL_PWD, {
          'email': account.value,
          'password': password.value,
          'vcode':validateCode

        }, NetWorkManager.methodPOST, DataCallBack(
            businessCallBack: (v) {
              Tips.showToast('恭喜找回，请重新输入新密码登录');
              isCheckSuccess.value = true;
              isCheck.value = true;
              Future.delayed(Duration(seconds: 2), () {
                // 1秒后自动跳转
                final loginController= Get.find<LoginController>();
                loginController.updateName(account.value);
                Get.toNamed(Routes.LOGIN);
                // 清理相关数据
                textEditor1.value.text ='';
                textEditor2.value.text ='';
                textEditor3.value.text ='';
                textEditor4.value.text ='';
                textEditor5.value.text ='';
                textEditor6.value.text ='';
                hiddenUnderLine1.value = false;
                hiddenUnderLine2.value = false;
                hiddenUnderLine3.value = false;
                hiddenUnderLine4.value = false;
                hiddenUnderLine5.value = false;
                hiddenUnderLine6.value = false;
                focusNode2.value.unfocus();
                focusNode3.value.unfocus();
                focusNode4.value.unfocus();
                focusNode5.value.unfocus();
                focusNode6.value.unfocus();
                focusNode1.value.requestFocus();
              });
            },
            errorCallBack: (v) {
              isCheckSuccess.value = false;
              Tips.showToast(v);
              isCheck.value = true;
            }

        ),showLoading: true);


      } else {

        NetWorkManager.getInstance().requestBussiness(NetWorkManager.FORGET_PHONE_PWD, {
          'phone': account.value,
          'password': password.value,
          'vcode':validateCode

        }, NetWorkManager.methodPOST, DataCallBack(
            businessCallBack: (v) {
              Tips.showToast('恭喜找回，请重新输入新密码登录');
              isCheckSuccess.value = true;
              isCheck.value = true;
              Future.delayed(Duration(seconds: 2), () {
                // 1秒后自动跳转
                final loginController= Get.find<LoginController>();
                loginController.updateName(account.value);
                Get.toNamed(Routes.LOGIN);
              });
            },
            errorCallBack: (v) {
              isCheckSuccess.value = false;
              isCheck.value = true;
              Tips.showToast(v);
            }

        ),showLoading: true);


      }

    }
  }


  void reTryValidateCode() {
    if(isEmailType.value){
      getEmailValidateCode();
    }else{
      getPhoneValidateCode();
    }
  }

  // 获取手机验证码
  void getPhoneValidateCode() async{
    NetWorkManager.getInstance().requestBussiness(NetWorkManager.SEND_PHONE_VERIFICATION_CODE, {
      'phone': account.value,
      'smsmode': 2

    }, NetWorkManager.methodPOST, DataCallBack(
        businessCallBack: (v) {
          afterSendCode();
        },
        errorCallBack: (v) {
          Tips.showToast(v);
        }

    ),showLoading: true);
  }

//   获取邮箱验证码
  void getEmailValidateCode() async{
    NetWorkManager.getInstance().requestBussiness(NetWorkManager.SEND_EMAIL_VERIFICATION_CODE, {
      'email': account.value,
      'codemode': 2

    }, NetWorkManager.methodPOST, DataCallBack(
        businessCallBack: (v) {
          afterSendCode();
        },
        errorCallBack: (v) {
          Tips.showToast(v);
        }

    ),showLoading: true);
  }

//
void jumpPreFocus(posi){
    if(posi =='6'){
      focusNode6.value.unfocus();
      focusNode5.value.requestFocus();
    }
    if(posi =='5'){
      focusNode5.value.unfocus();
      focusNode4.value.requestFocus();
    }
    if(posi =='4'){
      focusNode4.value.unfocus();
      focusNode3.value.requestFocus();
    }
    if(posi =='3'){
      focusNode3.value.unfocus();
      focusNode2.value.requestFocus();
    }
    if(posi =='2'){
      focusNode2.value.unfocus();
      focusNode1.value.requestFocus();
    }
}


  void afterSendCode(){
    Tips.showToast('验证码已发送！');
    textEditor1.value.text ='';
    textEditor2.value.text ='';
    textEditor3.value.text ='';
    textEditor4.value.text ='';
    textEditor5.value.text ='';
    textEditor6.value.text ='';
    hiddenUnderLine1.value = false;
    hiddenUnderLine2.value = false;
    hiddenUnderLine3.value = false;
    hiddenUnderLine4.value = false;
    hiddenUnderLine5.value = false;
    hiddenUnderLine6.value = false;
    focusNode2.value.unfocus();
    focusNode3.value.unfocus();
    focusNode4.value.unfocus();
    focusNode5.value.unfocus();
    focusNode6.value.unfocus();
    focusNode1.value.requestFocus();
    showTimeText.value = true;
    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
      if (timeCount.value < 1) {
        timer.cancel();
        showTimeText.value = false;
        timeCount.value = 60;
      } else {
        timeCount.value = timeCount.value - 1;
      }
    });
  }


}
